package com.coocaa.app.gamecenter.pages.arsenal;

import android.os.Bundle;
import android.os.RemoteException;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.coocaa.app.gamecenter.framework.GameCenterActivity;
import com.coocaa.app.gamecenter.pages.arsenal.data.ArsenalData;
import com.coocaa.app.gamecenter.pages.arsenal.data.WeaponData;
import com.coocaa.app.gamecenter.pages.arsenal.data.WeaponItemData;
import com.coocaa.x.framework.utils.LiteURI;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.service.litecontent.LiteContentServiceConnecter;
import com.coocaa.x.service.litecontent.data.com_coocaa_arsenal.RP_Arsenal_Info;
import com.tianci.appstore.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xfk on 2015/11/4.
 */
public class ArsenalActivity extends GameCenterActivity {

    ArsenalLayout arsenalLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showloading();
    }
    @Override
    protected View createContentView() {
        arsenalLayout = new ArsenalLayout(this);
        return arsenalLayout;
    }

    @Override
    protected void loadData() throws Exception {
        updateArsenalDatas();
        hideloading();
    }

    @Override
    protected void updateContentView() {
        updateTitle("军火库");
    }

    private ArsenalData getArsenalDataFromWeb() {
        ArsenalData arsenalData = null;

//        try {
//            Thread.sleep(4000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        LiteURI uri = LiteURI.create("store://com.coocaa.arsenal/arsenalInfo");
        try {
            String result = LiteContentServiceConnecter.getService().call(uri.toString());
            if(result == null)
                return null;
            LogUtils.i("arsenal", "result: "+result);

            RP_Arsenal_Info arsenal_info = JSONObject.parseObject(result, RP_Arsenal_Info.class);
            if(arsenal_info != null) {
                arsenalData = new ArsenalData();
                arsenalData.weaponClassTotal = arsenal_info.resource.total_count;
                LogUtils.i("arsenal", "total_count: "+arsenal_info.resource.total_count);
                List<WeaponData> weaponDataList = new ArrayList<WeaponData>();
                for (RP_Arsenal_Info.ArsenalClassData arsenalBean: arsenal_info.resource.resource_items) {

                    WeaponData weaponData = new WeaponData();
                    weaponData.name = arsenalBean.name;
                    List<WeaponItemData> weaponItemDataList = new ArrayList<WeaponItemData>();
                    if(arsenalBean.weaponList != null) {
                        LogUtils.i("arsenal", "bulletBean size = "+arsenalBean.weaponList.size());
                        for (RP_Arsenal_Info.WeaponItemData bulletBean: arsenalBean.weaponList) {
                            WeaponItemData weaponItemData = new WeaponItemData();
                            weaponItemData.price = bulletBean.price;
                            weaponItemData.middleUrl = bulletBean.middleUrl;
                            weaponItemData.smallUrl = bulletBean.smallUrl;
                            weaponItemData.bigUrl = bulletBean.bigUrl;
                            weaponItemDataList.add(weaponItemData);
                        }
                    }

                    weaponData.weaponList = weaponItemDataList;
                    weaponDataList.add(weaponData);
                }

                arsenalData.weaponDataList = weaponDataList;
            } else {
                //提示数据异常
            }
        } catch (RemoteException e) {
            arsenalData = null;
            e.printStackTrace();
        }

        return arsenalData;
    }

    private void updateArsenalDatas() {
        //调用后台接口获取军火库数据
        startSlowNetTimerDalay(7000);
        final ArsenalData arsenalData = getArsenalDataFromWeb();
        if(arsenalData == null || arsenalData.weaponDataList == null
                || arsenalData.weaponDataList.size() == 0) {
            LogUtils.i("arsenal", "updateArsenalDatas error");
            hideloading();
            cancelSlowNetTimer();
            if (!checkNet()) {
                setLoadDataErrorMsg(getString(R.string.connection_more_wonderful));
            } else {
                setLoadDataErrorMsg(getString(R.string.no_app_list));
            }

            if(arsenalLayout != null) {
                arsenalLayout.removeAllViews();
                arsenalLayout.onDestroy();
            }
            return;
        } else {
            cancelSlowNetTimer();
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                arsenalLayout.refreshUI(arsenalData);
            }
        });

    }

    @Override
    public String getName() {
        return "酷游吧军火库页";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        LogUtils.i("arsenal", "onDestroy");
        if(arsenalLayout != null) {
            arsenalLayout.onDestroy();
            arsenalLayout.destroyDrawingCache();
            arsenalLayout = null;
        }
    }
}
